package com.my.study.hutool.core.io;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.watch.WatchMonitor;
import cn.hutool.core.io.watch.Watcher;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.WatchEvent;

/**
 * @author: yidujun
 * @create: 2021/07/02 16:00
 */
@Slf4j(topic = "c.WatchMonitorTest")
public class WatchMonitorTest {

    public static void main(String[] args) {
        File file = FileUtil.file("my.properties");
        //  监听文件或目录的修改
        WatchMonitor watchMonitor = WatchMonitor.create(file, WatchMonitor.ENTRY_MODIFY);
        watchMonitor.setWatcher(new Watcher() {
            @Override
            public void onCreate(WatchEvent<?> event, Path currentPath) {
                Object context = event.context();
                log.debug("创建: {} -> {}", context, currentPath);
            }

            @Override
            public void onModify(WatchEvent<?> event, Path currentPath) {
                Object context = event.context();
                log.debug("修改: {} -> {}", context, currentPath);
            }

            @Override
            public void onDelete(WatchEvent<?> event, Path currentPath) {
                Object context = event.context();
                log.debug("删除: {} -> {}", context, currentPath);
            }

            @Override
            public void onOverflow(WatchEvent<?> event, Path currentPath) {
                Object context = event.context();
                log.debug("Overflow: {} -> {}", context, currentPath);
            }
        });

        watchMonitor.setMaxDepth(3);
        watchMonitor.start();
    }

}
